package com.cryptape.cita_wallet.net.common;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.cryptape.cita_wallet.view.dialog.ProgressingDialog;
import com.cryptape.cita_wallet.net.exception.ApiException;
import com.mangoworld.android.R;


import java.io.IOException;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class RxSubscriber<T> implements Observer<T> {

    //    private LoadingDialog mLoadingDialog;
    public ProgressingDialog mLoadingDialog = null;
    private Disposable disposable;
    private Context context;
    private String errorMsg;
    private boolean mShowLoading = false;

    public RxSubscriber(Context context, boolean showLoading) {
        this.context = context;
        this.mShowLoading = showLoading;
    }

    public RxSubscriber() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposable = new CompositeDisposable();
        if (mShowLoading) {
            showLoading();
        }
    }


    @Override
    public void onNext(T t) {
        dismissLoading();
        if (t == null || ObjectUtils.isEmpty(t)) {
            onFail("报null");
        } else {
            onSuccess(t);
        }
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof IOException) {
            /** 没有网络 */
            errorMsg = "Please check your network status";
        } else if (e instanceof HttpException) {
            /** 网络异常，http 请求失败，即 http 状态码不在 [200, 300) 之间, such as: "server internal error". */
            errorMsg = "errorBody:" + ((HttpException) e).response().errorBody()
                    + ";message:" + ((HttpException) e).response().message()
                    + ";code:" + ((HttpException) e).response().code();
        } else if (e instanceof ApiException) {
            /** 网络正常，http 请求成功，服务器返回逻辑错误 */
            errorMsg = ((ApiException) e).getDisplayMessage();
        } else {
            /** 其他未知错误 */
            errorMsg = !TextUtils.isEmpty(e.getMessage()) ? e.getMessage() : "unknown error";
        }

        dismissLoading();
       /* if (BuildConfig.DEBUG && !ObjectUtils.isEmpty(context)) {
            AlertDialog.Builder localBuilder = new AlertDialog.Builder(context);
            localBuilder.setTitle("提示");
            localBuilder.setMessage(errorMsg);
            localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int paramAnonymousInt) {
                    dialogInterface.dismiss();
                    if (!disposable.isDisposed()) {
                        disposable.dispose();
                    }
                }
            });
            localBuilder.setCancelable(false).create();
            localBuilder.show();
        }*/
        onFail(errorMsg);
    }


    public abstract void onFail(String failMsg);

    @Override
    public void onComplete() {
        dismissLoading();
        Log.d("print", "-->执行了完成的方法");
    }

    public abstract void onSuccess(T t);


    private void showLoading() {
        if (mLoadingDialog == null) mLoadingDialog = new ProgressingDialog(context);
        mLoadingDialog.setMsg(StringUtils.getString(R.string.loading));
        mLoadingDialog.show();
    }

    /**
     * 隐藏Progress Bar
     */
    private void dismissLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) mLoadingDialog.dismiss();
    }

}
